@extends('frontend/layouts/bootstrap')

@section('content')
<h2>Контейнер жагсаалт - Confirmed</h2>
<div id="notification"></div>
{{-- FILTER --}}
<div class="well well-small">
    <form class="form-inline" style="margin: 0;" action="{{ route('car_balance.container.confirmed') }}" method="get">
        {{-- CONTAINER SYS ID --}}
        @include('nimo.domains.car_balance.Main._form_field', array('field'=>'container_id', 'title' => 'Container System ID', 'value' => $filter_container_id, 'class' => 'input-medium', 'type' => 'text'))
        @include('nimo.domains.car_balance.Main._form_field', array('field'=>'container_no', 'title' => 'Container NO', 'value' => $filter_container_no, 'class' => 'input-medium', 'type' => 'text'))
        <input class="input-medium" placeholder="date1 - date2" type="text" name="container_date" value="{{ count($filter_container_date) > 1 ? $filter_container_date[0] .' - '.$filter_container_date[1] : '' }}"/>
@if (empty($customer)) <input class="input-medium" placeholder="memo" type="text" name="container_memo" value="{{$filter_container_memo}}"/> @endif
        <button type="submit" class="btn">Apply Filter</button>
        <a href="{{ route('car_balance.container.confirmed') }}" >Reset</a>
    </form>
</div>

@if ($containers->count())
        <table class="table table-bordered table-condensed table-hover">
            <thead style="background-color: #f0f0f0; white-space:nowrap;">
            <tr>
                <th title="Container System Id.">System ID.</th>
                <th title="Container No." colspan="2">コンテナ No.</th>
                <th title="B/L & Tulburiin nehemjleh" colspan="1">B/L & Attachments </th>
                <th title="Recycle date" colspan="1">Recycle date</th>
                <th title="Cars">Cars</th>
            </tr>
            </thead>
            <tbody style="font-size: 11px;">
            @foreach($containers as $container)
            {{ ''; $content_count=isset($container_contents[$container->container_id]) ? count($container_contents[$container->container_id]) : '0' }}
            @if (empty($customer))
            <tr>
                <td rowspan="{{ $content_count > 0 ? $content_count : 1 }}">{{ $container->container_id }}</td>
                <td rowspan="{{ $content_count > 0 ? $content_count : 1 }}">
                    No {{ $container->container_no or '-' }}<br/> Date {{ $container->container_date or '-' }}<br/> Departure: {{ $container->transport_start_date or 'Unknown'}}<br/>Arrival: {{ $container->transport_end_date or 'Unknown'}}
                    @if($content_count>0)  <br/>Totals: <?php $totals = 0; for($i = 1; $i < $content_count; $i++) { $totals+= intval($container_contents[$container->container_id][$i][3]);};?> {{number_format($totals/1000)}} @endif
                </td>
                <td rowspan="{{ $content_count > 0 ? $content_count : 1 }}">
                    @unless ($container->settlement_flag)
                    <a class="btn btn-mini btn-primary btn-settle" href="{{ route('car_balance.container.settle', $container->container_id) }}"><i class="icon icon-ok"></i> Mark as settled </a>
                    <!--<a class="btn btn-mini btn-success btn-addcar" href="#"><i class="icon icon-plus"></i> Add another car</a>-->
                    @endunless
                    <a class="btn btn-mini btn-primary btn-edit" href="{{ route('car_balance.container.edit', $container->container_id) }}"><i class="icon icon-edit"></i> Edit container details</a>
                    <a class="btn btn-mini btn-primary btn-details" href="{{ route('car_balance.container.detail', $container->container_id) }}"><i class="icon icon-zoom-in"></i> Show container car details</a>
                    @if ($container->memo) <br> {{ $container->memo }} @endif
                </td>
                <td rowspan="{{ $content_count > 0 ? $content_count : 1 }}">
                    <a class="btn btn-mini btn-primary btn-inverse" href="{{ route('car_balance.container.container_bill', $container->container_id) }}"><i class="icon icon-edit"></i> Upload bills</a>
                    @if (isset($container_attach_status[$container->attachid])) <br> <div style="max-width:200px;word-wrap: break-word;">{{ json_encode($container_attach_status[$container->attachid]) }} </div> @endif
                </td>
                <td rowspan="{{ $content_count > 0 ? $content_count : 1 }}">
                    @if ( $container->recycle_application_date )
                        <i class="icon icon-calendar"></i>
                        {{ $container->recycle_application_date }}</br>
                    @endif
                    <a class="btn btn-mini btn-primary btn-info" href="{{ route('car_balance.container.recycle', $container->container_id) }}"><i class="icon icon-eraser"></i> Set recycle date</a>
                </td>
                <td>
<a class="btn btn-primary btn-mini" href="{{ route('car_balance.car.car_show', $container_contents[$container->container_id][0][4]) }}" title="Detial"  target="cb_office"><i class="icon-zoom-in icon-white"></i></a>
<a class="btn btn-primary btn-mini" href="{{ route('car_balance.main.backoffice', $container_contents[$container->container_id][0][4]) }}" title="Expenses"  target="cb_office" ><i class="icon-edit icon-white"></i></a>                    
{{ $content_count ? $container_contents[$container->container_id][0][0] : '' }}&nbsp; {{ $content_count ? $container_contents[$container->container_id][0][1] : '' }}&nbsp; {{ $content_count ? $container_contents[$container->container_id][0][2] : '' }}
                </td>
            </tr>
            @elseif ($content_count)
            <tr>
                <td rowspan="{{ $content_count > 0 ? $content_count : 1 }}">{{ $container->container_id }}</td>
                <td rowspan="{{ $content_count > 0 ? $content_count : 1 }}">
                    {{ $container->container_no }}
                    @if ($container->transport_start_date)
                    <br/>Departure: {{ $container->transport_start_date }}
                    @endif
                    @if ($container->transport_end_date)
                    <br/>Arrival: {{ $container->transport_end_date }}
                    @endif
                </td>
                <td rowspan="{{ $content_count > 0 ? $content_count : 1 }}">
                    <a class="btn btn-mini btn-primary btn-details" href="{{ route('car_balance.container.detail', $container->container_id) }}"><i class="icon icon-zoom-in"></i> Show container car details</a>
                </td>
                <td rowspan="{{ $content_count > 0 ? $content_count : 1 }}"> - </td>
                <td rowspan="{{ $content_count > 0 ? $content_count : 1 }}"> - </td>
                <td>
                    {{ $content_count ? $container_contents[$container->container_id][0][0] : '' }}&nbsp;
                    {{ $content_count ? $container_contents[$container->container_id][0][1] : '' }}&nbsp;
                    {{ $content_count ? $container_contents[$container->container_id][0][2] : '' }}
                </td>
            </tr>
            @endif
            @if ($content_count)
                @for($i = 1; $i < $content_count; $i++)
                    <tr>
                        <td>
@if (empty($customer))
<a class="btn btn-primary btn-mini" href="{{ route('car_balance.car.car_show', $container_contents[$container->container_id][$i][4]) }}" title="Detial"  target="cb_office"><i class="icon-zoom-in icon-white"></i></a>
<a class="btn btn-primary btn-mini" href="{{ route('car_balance.main.backoffice', $container_contents[$container->container_id][$i][4]) }}" title="Expenses"  target="cb_office" ><i class="icon-edit icon-white"></i></a>
{{ $container_contents[$container->container_id][$i][0] }}&nbsp; {{ $container_contents[$container->container_id][$i][1] }}&nbsp; {{ $container_contents[$container->container_id][$i][2] }}
@else
{{ $container_contents[$container->container_id][$i][0] }}&nbsp; {{ $container_contents[$container->container_id][$i][1] }}&nbsp; {{ $container_contents[$container->container_id][$i][2] }}
@endif
                        </td>
                    </tr>
                @endfor
            @endif
            @endforeach
            </tbody>
        </table>

@include('nimo/domains/car_balance/page')

@else
    Хайлтад тохирох үр дүн алга
@endif

@stop


@section('pagestyles')
<link rel="stylesheet" type="text/css" href={{ asset('assets/chosen-bootstrap/chosen.min.css') }} />
<link rel="stylesheet" type="text/css" href={{ asset('assets/bootstrap-daterangepicker/daterangepicker.css') }} />
<link rel="stylesheet" type="text/css" href={{ asset('assets/bootstrap-datepicker/datepicker.css') }} />
<link rel="stylesheet" type="text/css" href={{ asset('assets/bootstrap-fileupload/fileupload.css') }} />

<!-- Modal -->
<div id="editModal" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <form method="post" action="">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h3>Edit container details</h3>
        </div>
        <div class="modal-body"></div>
        <div class="modal-footer">
            <a class="btn btn-primary btn-save" href="#">Save</a>
            <a class="btn btn-cancel" href="#" onclick="$('#editModal').modal('hide')">Cancel</a>
        </div>
    </form>
</div>

<!-- Recycle Modal -->
<div id="recycleModal" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <form method="post" action="">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h3>Set recycle application date</h3>
        </div>
        <div class="modal-body"></div>
        <div class="modal-footer">
            <a class="btn btn-primary btn-success" href="#">Save</a>
            <a class="btn btn-cancel" href="#" onclick="$('#recycleModal').modal('hide')">Cancel</a>
        </div>
    </form>
</div>

<!-- Detail Modal -->
<div id="detailModal" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="width: 800px;">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h3>Container car details</h3>
    </div>
    <div class="modal-body"></div>
</div>

@stop

@section('pagescripts')
<!--page specific plugin scripts-->
<script type="text/javascript" src={{ asset('assets/chosen-bootstrap/chosen.jquery.min.js') }}></script>
<script type="text/javascript" src={{ asset('assets/bootstrap-daterangepicker/date.js') }} ></script>
<script type="text/javascript" src={{ asset('assets/bootstrap-daterangepicker/daterangepicker.js') }}></script>
<script type="text/javascript" src={{ asset('assets/bootstrap-datepicker/datepicker.js') }}></script>
<script type="text/javascript">
    $(document).ready(function() {
        $('input[name="container_date"]').daterangepicker({format:"yyyy-MM-dd"});
    
        $('.btn-edit').on('click', function(e){
            e.preventDefault();
            $('#editModal').modal('show')
            $('#editModal .modal-body').html('Loading...');
            $('#editModal form').attr('action',$(this).attr('href'));
            $.get($(this).attr('href'), function(d){
                $('#editModal .modal-body').html(d);
            });
        });
        $('.btn-save').on('click', function(e){
            e.preventDefault();
            $.post($('#editModal form').attr('action'), $('#editModal form').serialize(), function(d){
                if(d.status == 'success')
                    $('#notification').html('<div class="alert alert-success">'+ d.message+'</div>')
                $('#editModal').modal('hide')
            })
        });
        $('.btn-settle').on('click', function(e){
            e.preventDefault();
            var $that = this;
            $.get($(this).attr('href'), function(d){
                if(d.status == 'success') {
                    $('#notification').html('<div class="alert alert-success">'+ d.message+'</div>')
                    location.reload();
                }
            })
        });
        $('.btn-remove-car').on('click', function(e){
            e.preventDefault();
            if(confirm('Are you sure?')) {
                $.get($(this).attr('href'), function(d){
                    if(d.status == 'success') {
                        $('#notification').html('<div class="alert alert-success">'+ d.message+'</div>')
                        location.reload();
                    }
                })
            }
        });

        $('.btn-info').on('click', function(e){
            e.preventDefault();
            $('#recycleModal').modal('show')
            $('#recycleModal .modal-body').html('Loading...');
            $('#recycleModal form').attr('action',$(this).attr('href'));
            $.get($(this).attr('href'), function(d){
                $('#recycleModal .modal-body').html(d);
            });
        });
        $('.btn-success').on('click', function(e){
            e.preventDefault();
            $.post($('#recycleModal form').attr('action'), $('#recycleModal form').serialize(), function(d){
                if(d.status == 'success')
                    $('#notification').html('<div class="alert alert-success">'+ d.message+'</div>')
                $('#recycleModal').modal('hide')
            })
        });

        $('.btn-details').on('click', function(e){
            e.preventDefault();
            $('#detailModal').modal('show')
            $('#detailModal .modal-body').html('Loading...');
            $('#detailModal form').attr('action',$(this).attr('href'));
            $.get($(this).attr('href'), function(d){
                $('#detailModal .modal-body').html(d);
            });
        });
        $params = $.param($( 'form input[name^=container]' ));
        $('.pagination li a').each(function(){
            $(this).attr("href",  $(this).attr("href")+'&'+$params);
         });
    });
</script>
@stop